#include "EAbstractView.h"

EAbstractView::EAbstractView(QQuickItem *parent)
    : QQuickPaintedItem(parent)
{

}

bool EAbstractView::getAcceptClick() const
{
    return acceptClick;
}

void EAbstractView::setAcceptClick(bool accept)
{
    if(acceptClick!=accept){
        acceptClick=accept;
        setAcceptedMouseButtons(acceptClick?(Qt::AllButtons):(Qt::NoButton));
        emit acceptClickChanged();
    }
}

bool EAbstractView::getAcceptHover() const
{
    return acceptHover;
}

void EAbstractView::setAcceptHover(bool accept)
{
    if(acceptHover!=accept){
        acceptHover=accept;
        setAcceptHoverEvents(acceptHover);
        emit acceptHoverChanged();
    }
}

QFont EAbstractView::getFont() const
{
    return font;
}

void EAbstractView::setFont(const QFont &font)
{
    this->font=font;
    emit fontChanged();
    refresh();
}

void EAbstractView::classBegin()
{
    acceptClick=true;
    setAcceptedMouseButtons(Qt::AllButtons);
    acceptHover=false;
    setAcceptHoverEvents(false);
}

void EAbstractView::componentComplete()
{
    for(QObject *child:this->children()) {
        if (EAbstractLayer *layer=qobject_cast<EAbstractLayer *>(child)) {
            layerList.push_back(layer);
            connect(layer,&EAbstractLayer::layerChanged,this,&EAbstractView::refresh);
        }
    }
    //根据z值升序排列，这样z值大的就会绘制到表面
    std::sort(layerList.begin(),layerList.end(),
              [this](EAbstractLayer *left,EAbstractLayer *right){
        return left->getZ()<right->getZ();
    });
}
